#!/usr/bin/env python
from misc.utility.scons_hints import *

import os

import editor_builders

Import("env")

env.editor_sources = []

if env.editor_build:
    # Generate doc data paths
    env.CommandNoCache(
        "doc/doc_data_class_path.gen.h",
        env.Value(env.doc_class_path),
        env.Run(editor_builders.doc_data_class_path_builder),
    )

    # Register exporters
    gen_exporters = env.CommandNoCache(
        "export/register_exporters.gen.cpp",
        env.Value(env.platform_exporters),
        env.Run(editor_builders.register_exporters_builder),
    )
    for e in env.platform_exporters:
        # Add all .cpp files in export folder
        env.add_source_files(env.editor_sources, f"../platform/{e}/export/*.cpp")

    # Core API documentation.
    docs = []
    docs += Glob("#doc/classes/*.xml")

    # Module API documentation.
    module_dirs = []
    for d in env.doc_class_path.values():
        if d not in module_dirs:
            module_dirs.append(d)

    for d in module_dirs:
        if not os.path.isabs(d):
            docs += Glob("#" + d + "/*.xml")  # Built-in.
        else:
            docs += Glob(d + "/*.xml")  # Custom.

    docs = sorted(docs)
    env.CommandNoCache(
        "#editor/doc/doc_data_compressed.gen.h",
        docs,
        env.Run(editor_builders.make_doc_header),
    )

    # Editor interface and class reference translations incur a significant size
    # cost for the editor binary (see godot-proposals#3421).
    # To limit it, we only include translations with a high enough completion
    # ratio (20% for the editor UI, 10% for the class reference).
    # Generated with `make include-list` for each resource.

    # Editor translations
    env.CommandNoCache(
        "#editor/translations/editor_translations.gen.h",
        Glob("#editor/translations/editor/*"),
        env.Run(editor_builders.make_translations_header),
    )

    # Property translations
    env.CommandNoCache(
        "#editor/translations/property_translations.gen.h",
        Glob("#editor/translations/properties/*"),
        env.Run(editor_builders.make_translations_header),
    )

    # Documentation translations
    env.CommandNoCache(
        "#editor/translations/doc_translations.gen.h",
        Glob("#doc/translations/*"),
        env.Run(editor_builders.make_translations_header),
    )

    # Extractable translations
    env.CommandNoCache(
        "#editor/translations/extractable_translations.gen.h",
        Glob("#editor/translations/extractable/*"),
        env.Run(editor_builders.make_translations_header),
    )

    env.add_source_files(env.editor_sources, "*.cpp")
    env.add_source_files(env.editor_sources, gen_exporters)

    SConscript("animation/SCsub")
    SConscript("asset_library/SCsub")
    SConscript("audio/SCsub")
    SConscript("debugger/SCsub")
    SConscript("doc/SCsub")
    SConscript("docks/SCsub")
    SConscript("export/SCsub")
    SConscript("file_system/SCsub")
    SConscript("gui/SCsub")
    SConscript("icons/SCsub")
    SConscript("inspector/SCsub")
    SConscript("import/SCsub")
    SConscript("plugins/SCsub")
    SConscript("project_manager/SCsub")
    SConscript("project_upgrade/SCsub")
    SConscript("run/SCsub")
    SConscript("settings/SCsub")
    SConscript("scene/SCsub")
    SConscript("script/SCsub")
    SConscript("shader/SCsub")
    SConscript("themes/SCsub")
    SConscript("translations/SCsub")
    SConscript("version_control/SCsub")

    lib = env.add_library("editor", env.editor_sources)
    env.Prepend(LIBS=[lib])
